package com.third.clock.ui.model.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.third.clock.R;
import com.third.clock.bean.MediaEntity;
import com.third.clock.bean.NoticeEntity;
import com.third.clock.bean.ReportEntity;
import com.third.clock.manager.ImgaeEngineManager;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/7/23 0023.
 */

public class NoticeDetailsAdapter extends RecyclerView.Adapter<NoticeDetailsAdapter.ViewHolder> {

    private Context context;

    private List<MediaEntity> list=new ArrayList<>();

    private ImgaeEngineManager mImgaeEngineManager;

    public NoticeDetailsAdapter(Context context){
        this.context = context;
        mImgaeEngineManager=new ImgaeEngineManager();
    }

    public void setList(List<MediaEntity> list){
        if(list!=null){
            this.list.clear();
            this.list.addAll(list);
            notifyDataSetChanged();
        }
    }

    public void addList(List<MediaEntity> list){
        if(list!=null){
            this.list.addAll(list);
            notifyDataSetChanged();
        }
    }

    public MediaEntity getEntity(int position){
        if(list!=null&&list.size()>position){
           return list.get(position);
        }
        return null;
    }

    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_noticedetails_layout,parent,false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {
        MediaEntity mediaEntity=list.get(position);
        if(mediaEntity!=null){
            String url=mediaEntity.getUrl();
            if(!TextUtils.isEmpty(url)){
                mImgaeEngineManager.loadImage(context,url,holder.mImageView);
            }
        }
    }

    @Override
    public int getItemCount() {
        return list==null?0:list.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder{

        private RelativeLayout layout;

        private ImageView mImageView;

        public ViewHolder(View itemView) {
            super(itemView);
            mImageView = (ImageView) itemView.findViewById(R.id.imageview_noticedetails);
            layout=(RelativeLayout)itemView.findViewById(R.id.imageview_noticedetails_layout);

            WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
            int width = wm.getDefaultDisplay().getWidth();
            int mLayoutWidth=width/3;
            int mLayoutHeight=mLayoutWidth;
            RelativeLayout.LayoutParams layoutParams=new RelativeLayout.LayoutParams(mLayoutWidth,mLayoutHeight);
            layout.setLayoutParams(layoutParams);
        }
    }



}
